<?php
namespace app\devel\controller;
use think\Controller;
use app\index\domain as domain;

class Test extends Controller
{
    private $arr;
    private $foreach;
    private $var;
    private $list;

    public function __construct()
    {
        parent::__construct();
        if(!APP_DEVEL)
            $this->redirect('/');
        \think\Config::set('log.type', 'test');
        include(APP_PATH.'index/common.php');
        $this->arr = $this->getArr(I('get.str'));
        $this->foreach = $this->getForeach(I('get.foreach'));
        //domain\Classes::cacheStart('test');
        $site = domain\Classes::get('Option');
        $links = domain\Classes::get('Links');
        $termList = domain\Classes::get('TermList', 0);
        $this->view->assign('termList',$termList);
        $this->view->assign('site',$site);
        $this->view->assign('links',$links);
    }
    
    public function __destruct()
    {
        //domain\Classes::cacheEnd('test');
    }

    /**
     * 显示首页
     */
    public function index()
    {
        return $this->fetch('index/index');
    }

    /**
     * 显示栏目页
     */
    public function term($termId=null)
    {
        if(is_null($termId)){
            $flag = I('get.flag');
            $termId   = D('index/Term')->getIdByFlag($flag);
        }
        $term = domain\Classes::get('Term', $termId);
        $tpl = D('index/TermModel')->getTpl($term['model_id'], 'term');
        if(empty($tpl))
            return $this->index();
        if($tpl == 'jump')
            return $this->term($term['child']['0']['id']);
        if($tpl == 'jump-content')
            return $this->content($term['content']['0']['id']);
        $this->assign('term', $term);
        trace('tpl='.$tpl.', id='.$termId,'log');
        return $this->fetch('index/'.$tpl);
    }

    /**
     * 显示内容页
     */
    public function content($contentId=null)
    {
        if(is_null($contentId)){
            $contentId = I('get.id');
        }
        $content = domain\Classes::get('Content', $contentId);
        $term    = $content['term'];

        $tpl = D('index/TermModel')->getTpl($term['model_id'], 'content');
        if(empty($tpl))
            return $this->index();
        $this->assign('term', $term);
        $this->assign('content', $content);
        trace('tpl='.$tpl,'log');
        return $this->fetch('index/'.$tpl);
    }

    /**
     * 显示搜索页
     */
    public function search()
    {
        $modelId = I('?post.mid')? I('post.mid'): 0;
        $search = domain\Classes::get('Search', $modelId);
        $this->assign('search', $search);
        return $this->fetch('index/search');
    }

    /**
     * 获取对应值
     */
    public function fetch($tpl='', $vars=[], $config=[])
    {
        if(empty($this->foreach))
            $this->getVar($this->arr);
        else
            $this->doForeach($this->foreach);
        $var = $this->var;
        return json_encode($result);
    }

    /**
     * 获取变量值
     */
    private function getVar($arr=null)
    {
        if(null===$arr)
            $arr = $this->arr;
        $var = array_shift($arr);
        if(strpos($var, '('))
            eval('$var='.$var.';');
        else
            $var = $this->view->$var;
        if(!empty($this->arr))
            foreach($this->arr as $vo)
                $var = $var[$vo];
        return $var;
    }

    /**
     * 将变量装入待处理队列
     */
    private function doVar($key=null){
        $var = $this->getVar();
        //处理变量
        $tags = [];
        $isArr= 0;
        switch (gettype($var)) {
        case 'object':
            $tags = $this->getObjTags($var);
            if($var instanceof domain\BaseList)
                $isArr = 1;
            break;
        case 'array':
            $tags = $this->getArrTags($var);
            $isArr = 1;
            break;
        }
        $result['content'] = $var;
        $result['tags'] = $tags;
        $result['isArr'] = $isArr;
        if($key)
            $this->view->_vars[$key] = $result;
        else
            $this->view->_vars[] = $result;
    }

    /**
     * 获取对应值
     */
    private function getArr($var)
    {
        $var = str_replace(' ', '', $var);
        $var = str_replace('$', '', $var);
        $arr = explode(".",$var);
        return $arr;
    }

    /**
     * 解析传入的Foreach字符串
     */
    private function getForeach($var)
    {
        if(empty($var)) return [];
        $var = str_replace(' ', '', $var);
        $var = str_replace('$', '', $var);
        $arr = explode(":foreach",$var);
        foreach($arr as &$vo)
            if(1<count($vo = explode('as', $vo)))
                $vo[0] = explode(".",$vo[0]);
            else
                return false;
        unset($vo);
        return $arr;
    }

    /**
     * 解析Foreach数组
     */
    private function doForeach($foreach)
    {
        list($arr, $var) = array_shift($foreach);
        $arr = $this->getVar($arr);
        $this->view->_list[] = $arr;
        foreach($arr as $vo){
            $this->view->$var = $vo;
            if(empty($foreach))
                $this->doVar();
            else
                $this->doForeach($foreach);
        }
    }

    /**
     * 获取某个对象的连贯标签
     */
    private function getObjTags($var)
    {
//        $tags = [];
//        $map = [
//            'Term' => [],
//            'TermList' => [],
//            'Content' => [],
//            'ContentList' => [],
//        ];
//        foreach($map as $key=>$vo)
//            if($var instanceof $key)
//                $tags = $vo;
//        if($var instanceof BaseList)
//            foreach($var as $key=>$vo)
//                $tags[] = $key;
//        return $tags;
        return $var->getTags();
    }

    /**
     * 获取某个数组的连贯标签
     */
    private function getArrTags($var)
    {
        return array_keys($var);
    }
}

